package com.mew.game.chess;

import com.mew.common.rest.game.BetAfterRequest;
import com.mew.game.strategy.StrategyService;
import com.zebra.common.concurrent.ZebraExecutorManager;
import com.zebra.common.event.EventBusAsyncCenter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.stream.Collectors;

@RestController
@Slf4j
public class ChessBetStrategyController {

    @Autowired
    private StrategyService strategyDataService;


    @PostMapping(value = "/chess/bet/after")
    @ResponseBody
    public Integer chessBetAfter(@RequestBody List<BetAfterRequest> request){
        ExecutorService executor =  ZebraExecutorManager.getInstance().getDefaultExecutor();
        if (null != executor){
            executor.execute(() -> doDataBack(request));
        }else {
            doDataBack(request);
        }
        return 1;
    }

    private void doDataBack(List<BetAfterRequest> request){
        try {
            for (BetAfterRequest afterRequest : request){
                log.info("Chess after {}", request.toString());
                strategyDataService.dataBack(afterRequest);
            }
            EventBusAsyncCenter.post(request.stream().map(BetAfterRequest::toPlayChessSasRequest).collect(Collectors.toList())); //TODO
        }catch (Exception e){
            log.error("ChessBetAfter Exception", e);
        }

    }



}
